package com.attilax.acc;

import java.math.BigDecimal;
import java.util.Map;

import com.attilax.db.DBX;
import com.attilax.lang.text.strUtil;
import com.attilax.sql.EmptyRzt;
import com.attilax.sql.SqlCheckor;
import com.attilax.sql.SqlExeService;
import com.google.common.collect.Maps;
import com.google.inject.Inject;

public class AccSrv4game  extends AccSrvAbs{
	@Inject
	SqlExeService dbx;
	public AccSrv4game() {
		//super();
	 
	}

	public int addAmount(String uid, BigDecimal money) throws AccNotExist {
		String sql = strUtil.fmt( "select * from account where id='$uid$'",uid);
		System.out.println( "--sql:"+sql);
		Map row;
		try {
			row = dbx.uniqueResult2row(sql);
		} catch (EmptyRzt e) {
			throw new AccNotExist("uid:"+uid);
		}
		if(row==null)
			throw new CantFindAcc("uid="+uid);
		BigDecimal now= new BigDecimal( row.get("totalrmb").toString());
		
//		SqlCheckor sqlCkSrv=
	//	if(row.get("vali_val")!=null)   //for compati  olddata no vali
	//	sqlCkSrv.valideValCheck4oldrow(row.get("user_money"),row.get("vali_val").toString() );
		
		BigDecimal  new_money=now.add(money);
//		String new_money_vali= new SqlCheckor().getValideVal(new_money);
//		new SqlCheckor().valideValCheck4newRow(new_money, new_money_vali);
		
			//DataLogicSales
			// TODO Auto-generated method stub
			Map m=Maps.newHashMap();
			m.put(5, 2);	m.put(20, 9);	m.put(50, 25);m.put(100, 60);m.put(0.01, 1);
			int addCard=(int) m.get(money.intValue());
			
			 /* 鏇存柊鐢ㄦ埛淇℃伅 */
		  String sql2 = "UPDATE   account "+
		            " SET totalrmb =$newMoney$,card=card+$card$"+
		           
		            " WHERE id = '"+uid+"' LIMIT 1";
		String   $sql = sql2.replace("$newMoney$", new_money.toString()).replace("$card$",String.valueOf( addCard));
		  
		  System.out.println("---"+  $sql);
			
		  
		int r=	dbx.execSql_retInt($sql);
		if(r!=1)
			throw new AccEx(" acc ex ret is:"+String.valueOf(r) +" uid:"+uid);
		System.out.println("----ret int:"+String.valueOf(r));
			return r;
		
	}

}